package com.szly.phm.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szly.phm.common.entity.dto.parameter.CreateParameterDTO;
import com.szly.phm.common.entity.dto.parameter.ParameterDTO;
import com.szly.phm.common.entity.dto.parameter.UpdateParameterDTO;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.po.SysParameter;
import com.szly.phm.common.entity.result.CodeStatusEnum;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.parameter.ParameterVO;
import com.szly.phm.common.mapper.SysParameterMapper;
import com.szly.phm.common.mapper.SysParameterTypeMapper;
import com.szly.phm.common.service.ISysParameterService;
import com.szly.phm.common.utils.BeanCopyUtils;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

/**
 * phm
 *
 * @author: git.jas0nch
 * date: 2021-10-11
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysParameterServiceImpl extends ServiceImpl<SysParameterMapper, SysParameter> implements ISysParameterService {
        private final SysParameterTypeMapper sysParameterTypeMapper;
    @Override
    public RootResponse<PageResult<ParameterVO>> queryParameterPage(ParameterDTO parameterDTO) {
       var newList=new ArrayList<ParameterVO>();
        var wrapper = new LambdaQueryWrapper<SysParameter>();
        //
        wrapper.eq(SysParameter::getIsSoftDelete, false);
        // fuzzy query 模糊查询；
        if (parameterDTO.getParameterName()!=null){
            // if not null, then get the specially query by the name;
            wrapper.like(SysParameter::getParameterName,parameterDTO.getParameterName());
        }
        //
        wrapper.orderByDesc(SysParameter::getCreateDateTime, SysParameter::getId);
        // page helper
        PageHelper.startPage(parameterDTO.getPageNum(), parameterDTO.getPageSize());
        var parameter = baseMapper.selectList(wrapper);
        var pageInfo = PageInfo.of(parameter);
        var pageList = pageInfo.getList();
        pageList.forEach(p->{
          var parameterVO=  new ParameterVO();
          BeanCopyUtils.copy(p,parameterVO);
          parameterVO.setParameterTypeName( sysParameterTypeMapper.selectById(p.getParameterTypeId()).getCodeName());
            newList.add(parameterVO);
        });
//        System.out.println(pageList);//
        return RootResponse.success(PageResult.page(newList, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal()));

    }

    @Override
    public RootResponse createParameter(CreateParameterDTO createParameterDTO, LoginUserDTO userDTO) {
        //检查重名
        var wrapper = new LambdaQueryWrapper<SysParameter>();
        wrapper.eq(SysParameter::getParameterName, createParameterDTO.getParameterName());
        wrapper.eq(SysParameter::getIsSoftDelete, false);
        var parameter = baseMapper.selectOne(wrapper);
        if (parameter != null) {
            return RootResponse.error("系统参数名称已存在，请勿重复添加");
        }
        parameter = new SysParameter();
//        BeanCopyUtils.copy(src,dest);
        BeanCopyUtils.copy(createParameterDTO, parameter);
        parameter.setId(HandleUtils.UUID32());
        parameter.setCreator(userDTO.getUsername());
        parameter.setCreateDateTime(new Date());
        parameter.setIsSoftDelete(false);
        int i = baseMapper.insert(parameter);
        return i == 1 ? RootResponse.success(parameter) : RootResponse.error();
    }

    @Override
    public RootResponse updateParameter(UpdateParameterDTO updateParameterDTO, LoginUserDTO userDTO) {
        var wrapper1 = new LambdaQueryWrapper<SysParameter>();
        wrapper1.eq(SysParameter::getId, updateParameterDTO.getId());
        wrapper1.eq(SysParameter::getIsSoftDelete, false);
        var parameter = baseMapper.selectOne(wrapper1);
        if (parameter == null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        //检查重名
        if (!updateParameterDTO.getParameterName().equals(parameter.getParameterName())) {
            wrapper1.clear();
            wrapper1.eq(SysParameter::getIsSoftDelete, false);
            wrapper1.eq(SysParameter::getParameterName,updateParameterDTO.getParameterName());
            var parameter2 = baseMapper.selectOne(wrapper1);
            if (parameter2 != null) {
                return RootResponse.error("系统参数名称已存在，请勿重复添加");
            }
        }
        BeanCopyUtils.copy(updateParameterDTO, parameter);
        parameter.setUpdater(userDTO.getUsername());
        parameter.setUpdateDateTime(new Date());
        int i = baseMapper.updateById(parameter);
        return i == 1 ? RootResponse.success(parameter) : RootResponse.error();
    }

    /**
     * 
     * @param id : the id to Parameter id;
     * @param userDTO : the user;
     * @return : the resultSet;
     */
    @Override
    public RootResponse deleteParameter(String id, LoginUserDTO userDTO) {
        var wrapper = new LambdaQueryWrapper<SysParameter>();
        wrapper.eq(SysParameter::getId, id);
        wrapper.eq(SysParameter::getIsSoftDelete, false);
        var parameter = baseMapper.selectOne(wrapper);
        if (parameter== null) {
            return RootResponse.error(CodeStatusEnum.NOT_EXIST_DATA);
        }
        parameter.setIsSoftDelete(true);
        parameter.setUpdateDateTime(new Date());
        parameter.setUpdater(userDTO.getUsername());
        baseMapper.updateById(parameter);
        return RootResponse.success(parameter);
    }
}
